package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.HotKeysBean;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.KeyboardUtils;
import com.rh.bmw4s.utills.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.edit_search_search)
    EditText editSearch;
    @BindView(R.id.img_search_delete)
    ImageView imgDelete;
    @BindView(R.id.btn_search_cancel)
    TextView btnCancel;
    @BindView(R.id.flow_search_hotkey)
    TagFlowLayout flowHotkey;

    private Gson gson;
    private int searchType;  //1 全局搜索  2 二手车搜索 3新车  4 汽车用品  5汽车轮胎
    private int cateId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        gson = new Gson();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            searchType = bundle.getInt("searchtype");
            cateId = bundle.getInt("cat_id");
        }

        editSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    editSearch.setCursorVisible(true);
                }
                return false;
            }
        });

        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String string = editSearch.getText().toString();
                if (!TextUtils.isEmpty(string)) {
                    imgDelete.setVisibility(View.VISIBLE);
                } else {
                    imgDelete.setVisibility(View.GONE);
                }
            }
        });

        //点击软键盘搜索键
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    KeyboardUtils.hideSoftInput(SearchActivity.this);
                    Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                    switch (searchType) {
                        case 1:
                            intent.putExtra("searchUrl", 1);
                            intent.putExtra("searchKey", editSearch.getText().toString());
                            break;
                        case 2:
                            intent.putExtra("searchUrl", 2);
                            intent.putExtra("searchKey", editSearch.getText().toString());
                            intent.putExtra("searchCarId", cateId);
                            break;
                        case 3:
                            intent.putExtra("searchUrl", 3);
                            intent.putExtra("searchKey", editSearch.getText().toString());
                            break;
                        case 4:
                            intent.putExtra("searchUrl", 4);
                            intent.putExtra("searchKey", editSearch.getText().toString());
                            break;
                        case 5:
                            intent.putExtra("searchUrl", 5);
                            intent.putExtra("searchKey", editSearch.getText().toString());
                            break;
                    }
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

        initHotKey();
    }

    /**
     * 热门标签
     */
    private void initHotKey() {
        DialogUtils.showProgressDlg(SearchActivity.this, "加载热门标签....");
        OkGo.<String>get(Constant.HotKeys)
                .tag(SearchActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        final HotKeysBean keysBean = gson.fromJson(response.body().toString(), HotKeysBean.class);
                        if (keysBean.getStatus() == Constant.SUCESS) {
                            if (keysBean.getDatas() != null && keysBean.getDatas().size() > 0) {
                                //添加热门标签数据
                                flowHotkey.setAdapter(new TagAdapter(keysBean.getDatas()) {
                                    @Override
                                    public View getView(FlowLayout parent, int position, Object o) {
                                        String tip = keysBean.getDatas().get(position).getGoods_name();
                                        TextView textView = (TextView) LayoutInflater.from(SearchActivity.this)
                                                .inflate(R.layout.item_search_hot, flowHotkey, false);
                                        textView.setText(tip);
                                        return textView;
                                    }
                                });
                                //热门标签点击事件
                                flowHotkey.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                                    @Override
                                    public boolean onTagClick(View view, int position, FlowLayout parent) {
                                        String goods_name = keysBean.getDatas().get(position).getGoods_name();
                                        editSearch.setText("");
                                        editSearch.setText(goods_name);
                                        KeyboardUtils.showSoftInput(SearchActivity.this);
                                        return false;
                                    }
                                });
                            } else {
                                ToastUtils.toastForShort(SearchActivity.this, "暂无热门标签数据");
                            }
                        } else {
                            ToastUtils.toastForShort(SearchActivity.this, "请求热门标签数据失败");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(SearchActivity.this, "请求热门标签数据失败");
                    }
                });


    }

    @OnClick({R.id.img_search_delete, R.id.btn_search_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_search_delete:
                String editString = editSearch.getText().toString();
                if (!TextUtils.isEmpty(editString)) {
                    editSearch.setText("");
                }
                break;
            case R.id.btn_search_cancel:
                KeyboardUtils.hideSoftInput(SearchActivity.this);
                finish();
                break;
        }
    }

}
